package com.books.fragment


import android.content.Intent
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import base.QFragment
import base.QLoading
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SPStaticUtils
import com.books.R
import com.books.activity.BookReadActivity
import com.books.adapter.BooksLrytAdapter
import com.books.adapter.BooksTypeAdapter
import com.books.databinding.BooksFrgmentBinding
import com.books.model.BookHomeListModel
import com.books.model.BookViewModel
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.footer.ClassicsFooter
import com.scwang.smartrefresh.layout.header.ClassicsHeader
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.coroutines.launch
import model.BookLRYSearch
import java.util.*

class BooksFragment() : QFragment<BooksFrgmentBinding>() {
    private val typeList = ArrayList<String>()
    private var booksTypeAdapter: BooksTypeAdapter? = null
    private var type = "都市"
    private var pageLRY = 1
    private val pageSizeLRY = 10
    private var booksLrytAdapter: BooksLrytAdapter? = null
    private var qLoading: QLoading? = null
    private var total = 0


    var viewModel: BookViewModel? = null;


    override fun getLayoutRes(): Int {
        return R.layout.books_frgment

    }


    override fun initView() {
        viewModel = ViewModelProvider(this).get(BookViewModel::class.java)
    }

    override fun initData() {

        typeList.clear()
        typeList.add("都市")
        typeList.add("玄幻")
        typeList.add("奇幻")
        typeList.add("历史")
        typeList.add("科幻")
        typeList.add("军事")
        typeList.add("游戏")

        val layoutManager = LinearLayoutManager(
            context
        )
        layoutManager.orientation = LinearLayoutManager.HORIZONTAL
        binding!!.rvType.layoutManager = layoutManager
        booksTypeAdapter = BooksTypeAdapter(R.layout.books_type_item, typeList)
        binding!!.rvType.adapter = booksTypeAdapter


        // 设置刷新头部和尾部为经典样式
        binding!!.swipeLry.setRefreshHeader(ClassicsHeader(context))
        binding!!.swipeLry.setRefreshFooter(ClassicsFooter(context))
        val lryLayoutManager = GridLayoutManager(
            activity, 2, GridLayoutManager.VERTICAL, false
        )

        lryLayoutManager.orientation = LinearLayoutManager.VERTICAL
        binding!!.rvBook.layoutManager = lryLayoutManager
        booksLrytAdapter = BooksLrytAdapter(R.layout.cartoon_content_item)
        binding!!.rvBook.adapter = booksLrytAdapter


        // 设置刷新头部和尾部为经典样式
        binding!!.swipeLry.setRefreshHeader(ClassicsHeader(context))
        binding!!.swipeLry.setRefreshFooter(ClassicsFooter(context))
    }

    override fun initListener() {
        binding!!.swipeLry.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                //下拉刷新
                LogUtils.d("下拉刷新")
                if (pageLRY * 20 < total) {
                    LogUtils.d("下拉刷新B")
                    pageLRY = pageLRY + 1
                    networkMessage()
                } else {
                    binding!!.swipeLry.finishLoadMoreWithNoMoreData()
                    LogUtils.d("下拉刷新CC")
                }
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                //上拉
                LogUtils.d("上拉刷新")
                //                pageNo = 1;
                networkMessage()
                refreshLayout.finishRefresh()
            }
        })

        booksTypeAdapter!!.setOnItemClickListener { adapter, view, position ->
            val item = adapter.data[position] as String
            type = item
            yhList.clear()
            booksTypeAdapter!!.setIndex(position)
            booksTypeAdapter!!.notifyDataSetChanged()
            networkMessage()
        }

        booksLrytAdapter!!.setOnItemClickListener { adapter, view, position ->
            val item = adapter.data.get(position) as BookHomeListModel.DataBean

            var intent: Intent = Intent(activity, BookReadActivity::class.java);
            intent.putExtra("name",item.name)
            activity!!.startActivity(intent)

        }
    }


    private val yhList = ArrayList<BookLRYSearch.DataBean>()




    override fun networkMessage() {

        //发起网络请求
        lifecycleScope.launch {
            viewModel!!.getBookHomeList(type)
        }

        //网络请求监听
        lifecycleScope.launch {
            viewModel!!.bookHomeList.collect {
                LogUtils.d("我这个是B" + it.data.toString())
                booksLrytAdapter!!.replaceData(it.data)
            }
        }


    }

    override fun onResume() {
        super.onResume()

        pageLRY = 1
    }
}

